using Firestarter.Test.Base;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using FireStarterModeller.Test.BO;
using FireStarterModeller.UI.Relationship;
using Habanero.Testing.Base;
using Habanero.Faces.Base;
using Habanero.Faces.Win;
using Habanero.Util;
using Microsoft.Practices.Unity;
using NUnit.Framework;
using Rhino.Mocks;

namespace FireStarterModeller.Test.UI.Relationships
{


//Look At related Class
//if Owner Class and Related Class has Single Prop PrimaryKey
//
//  - Look for prop named OwnerClass + ID
//  if found then
//   - 1:M cardinality
//   - RelattionshipName = Pluralise of RelatedClass
//   - MyRelatedProp = IDProp
//   - RelatedProp = foundProp
//  The Remainder are Defaults
//	== Create Reverse Relationship with names etc.
//  	-- Show the user the std Relationship screen and allow them to edit anything.
//  
//  
//  Else
//   - If Related Class has single Prop ID
//     Look for Prop in OwnerClass = RelatedClass + ID
//     If found 
//	- M:1 cardinality
//   	- RelattionshipName = Singularise of RelatedClass
//   	- MyRelatedProp = foundProp
//   	- RelatedProp = RelatedProp.IDProp
// 	Remainder Default
// 	Create Reverse Rel
//     Show The std Relationship screen allow edits.
//
//  Else
//	- Screen 2 : Get Cardinality from user
//	- Screen 2 : Ask if you want to create props.
//	- If yes
//		Create Props
//		if 1:M Create Prop on RelatedClass
//			- ReverseName + ID (type == correspondingProp Type)
//		  Set Relatedprops
//		   - MyRelatedProp = IDProp
//   		   - RelatedProp = newProp
//		if M:1 Create Props On OwnerClass
//			- RelationShipName + ID (type == correspondingProp Type)
//		  Set Relatedprops
//		   - MyRelatedProp = newProp
//   		   - RelatedProp = RelatedClass.IDProp

    // 2010/03/15 Deerasha
    //  If Owner class is related to itself:
    //      Primary key should not be related to primary key.
    //      go to step 2 to allow user to setup related props?

    [TestFixture]
    public class TestCreateRelationshipWizardController
    {
        [SetUp]
        public void SetupTest()
        {
            //Runs every time that any testmethod is executed
            TestUtilsDomain.SetupTest();
            GlobalUIRegistry.ControlFactory = new ControlFactoryWin();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.
            TestUtilsDomain.SetupFixture();
        }

        [TearDown]
        public void TearDownTest()
        {
            //runs every time any testmethod is complete
        }

        [Test]
        public void Test_CreateController()
        {
            //---------------Set up test pack-------------------
            
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            DMClass dmClass = TestUtilsDMClass.CreateSavedDMClass();
            var cntrller = CreateDeleteDMClasWizardController(dmClass);
            //---------------Test Result -----------------------
            CreateRelationshipWizardStep1 step1 = (CreateRelationshipWizardStep1)cntrller.GetFirstStep();
            step1.InitialiseStep();
            Assert.AreSame(dmClass, step1.CreatedRelationship.ClassDM);
            Assert.IsNotNull(cntrller.RelationshipCreator);
        }

        private static CreateRelationshipWizardController CreateDeleteDMClasWizardController(DMClass dmClass)
        {
            var container = new UnityContainer();
            return new CreateRelationshipWizardController(container, dmClass);
        }
        [Test]
        public void TestWizardControllerSetup()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateSavedDMClass();
            var cntrller = GetController(dmClass);
            IWizardControl wzrdControl = GlobalUIRegistry.ControlFactory.CreateWizardControl(cntrller);
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            wzrdControl.Start();
            //---------------Test Result -----------------------
            Assert.AreEqual(4, cntrller.StepCount);
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep1), cntrller.GetFirstStep());
            Assert.IsTrue(cntrller.IsFirstStep());
            Assert.IsFalse(cntrller.IsLastStep());
        }

        [Test]
        public void Test_CompleteCurrentStep_WhenSecondStepCanMoveOn_ShouldCreateForeignKeyProps()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateSavedDMClass();
            TestUtilsShared.CreateSavedObjectIdentityInfo(dmClass);
            var cntrller = GetController(dmClass);
            SetupStep1(cntrller);
            DMRelationshipCreator creator = cntrller.RelationshipCreator;
            Assert.IsFalse(cntrller.RelationshipCreator.CanFindForeignKey);
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep1), cntrller.GetCurrentStep());
            Assert.IsFalse(cntrller.RelationshipCreator.CanFindForeignKey);
            IWizardStep step = cntrller.GetNextStep();
            Assert.IsFalse(cntrller.RelationshipCreator.CanFindForeignKey);
            step.InitialiseStep();
            DMRelationship relationship = creator.Relationship;
            //---------------Assert Precondition----------------
            Assert.IsFalse(creator.CanFindForeignKey);
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep2), step);
            Assert.AreEqual(0, relationship.RelationshipProperties.Count);
            //---------------Execute Test ----------------------
            CreateRelationshipWizardStep2 step2 = (CreateRelationshipWizardStep2) step;
            step2.CardinalityComboBox.SelectedItem = Cardinality.Multiple.ToString();
            cntrller.CompleteCurrentStep();
            //---------------Test Result -----------------------
            Assert.IsNotNull(relationship);
            Assert.AreEqual(Cardinality.Multiple, relationship.Cardinality);
            Assert.AreEqual(StringUtilities.Pluralize(relationship.RelatedClass.ClassNameBO), relationship.RelationshipName);
            Assert.AreEqual(1, relationship.RelationshipProperties.Count);
        }

        [Test]
        public void Test_GetNextStep_WhenStep2_ShouldReturnStep3()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateSavedDMClass();
            TestUtilsShared.CreateSavedObjectIdentityInfo(dmClass);
            var cntrller = GetController(dmClass);
            SetupStep1(cntrller);
            IWizardStep step = cntrller.GetNextStep();
            step.InitialiseStep();
            CreateRelationshipWizardStep2 step2 = (CreateRelationshipWizardStep2)step;
            step2.CardinalityComboBox.SelectedItem = Cardinality.Multiple.ToString();
            string message;
            bool canMoveOn = cntrller.CanMoveOn(out message);
            //---------------Assert Precondition----------------
            Assert.IsTrue(canMoveOn);
            //---------------Execute Test ----------------------
            IWizardStep wizardStep = cntrller.GetNextStep();
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep3), wizardStep);
        }

        [Test]
        public void Test_GetPreviousStep_WhenStep3_AndHadStep2_ShouldReturnStep2()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateSavedDMClass();
            TestUtilsShared.CreateSavedObjectIdentityInfo(dmClass);
            var cntrller = GetController(dmClass);
            SetupStep1(cntrller);
            IWizardStep step = cntrller.GetNextStep();
            step.InitialiseStep();
            CreateRelationshipWizardStep2 step2 = (CreateRelationshipWizardStep2)step;
            step2.CardinalityComboBox.SelectedItem = Cardinality.Multiple.ToString();
            string message;
            cntrller.CanMoveOn(out message);
            IWizardStep wizardStep = cntrller.GetNextStep();
            //---------------Assert Precondition----------------
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep3), wizardStep);
            //---------------Execute Test ----------------------
            IWizardStep previousStep = cntrller.GetPreviousStep();
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof (CreateRelationshipWizardStep2), previousStep);
        }

        [Test]
        public void Test_CancelWizard_ShouldCallCreator_CancelAllEdits()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateSavedDMClass();
            TestUtilsShared.CreateSavedObjectIdentityInfo(dmClass);
            var cntrller = GetController(dmClass);
            SetupStep1(cntrller);
            IWizardStep step = cntrller.GetNextStep();
            step.InitialiseStep();
            CreateRelationshipWizardStep2 step2 = (CreateRelationshipWizardStep2)step;
            step2.CardinalityComboBox.SelectedItem = Cardinality.Multiple.ToString();
            string message;
            cntrller.CanMoveOn(out message);
            //---------------Assert Precondition----------------
            Assert.IsNotNull(cntrller.RelationshipCreator.Relationship.OwnerClass);
            //---------------Execute Test ----------------------
            cntrller.CancelWizard();
            //---------------Test Result -----------------------
            Assert.IsNull(cntrller.RelationshipCreator.Relationship.OwnerClass);
            Assert.IsNull(cntrller.RelationshipCreator.Relationship.RelatedClass);
        }



        private static void SetupStep1(CreateRelationshipWizardController cntrller)
        {
            DMClass relatedClass = TestUtilsDMClass.CreateSavedDMClass(cntrller.RelationshipCreator.Relationship.OwnerClass.Solution);
            TestUtilsShared.CreateSavedObjectIdentityInfo(relatedClass);
            CreateRelationshipWizardStep1 step1 = (CreateRelationshipWizardStep1) cntrller.GetFirstStep();
            step1.InitialiseStep();
            step1.RelatedClassComboBox.SelectedItem = relatedClass;
            string message;
            step1.CanMoveOn(out message);
            return;
        }

        #region if Not Owner Class and Related Class has Single Prop PrimaryKey

        [Test]
        public void Test_GetNextStep_WhenFirstStep_OwnerClassHasCompositePrimaryKey_ShouldReturnStep2()
        {
            //---------------Set up test pack-------------------
            DMObjectIdentity identity = TestUtilsDMObjectIdentity.CreateUnsavedValidCompositeDMObjectIdentity();
            DMClass dmClass = identity.Class;
            DMClass relatedClass = TestUtilsDMClass.CreateDMClassWithObjectIdentity(dmClass.Solution);
            var cntrller = GetController(dmClass);
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, dmClass.ObjectIdentity.Properties.Count);
            Assert.IsTrue(dmClass.IsObjectIDValid);
            Assert.IsTrue(relatedClass.IsObjectIDValid);

            //---------------Execute Test ----------------------
            CreateRelationshipWizardStep1 step = (CreateRelationshipWizardStep1) cntrller.GetFirstStep();
            step.InitialiseStep();
            step.RelatedClassComboBox.SelectedItem = relatedClass;
            //---------------Test Result -----------------------
            string message;
            Assert.IsTrue(step.CanMoveOn(out message), message);

            Assert.IsInstanceOf(typeof (CreateRelationshipWizardStep1), cntrller.GetCurrentStep());
            Assert.IsInstanceOf(typeof (CreateRelationshipWizardStep1), cntrller.GetFirstStep());
            Assert.IsInstanceOf(typeof (CreateRelationshipWizardStep2), cntrller.GetNextStep());
        }

        [Test]
        public void Test_GetNextStep_WhenFirstStep_RelatedClassHasCompositePrimaryKey_ShouldReturnStep2()
        {
            //---------------Set up test pack-------------------
            DMObjectIdentity identity = TestUtilsDMObjectIdentity.CreateUnsavedValidCompositeDMObjectIdentity();
            DMClass relatedClass = identity.Class;
            DMClass dmClass = TestUtilsDMClass.CreateSavedDMClass(relatedClass.Solution);
            dmClass.ObjectIdentity = TestUtilsDMObjectIdentity.CreateUnsavedValidDMObjectIdentity_NoDMClass();
            var cntrller = GetController(dmClass);
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, relatedClass.ObjectIdentity.Properties.Count);
            //---------------Execute Test ----------------------
            CreateRelationshipWizardStep1 step = (CreateRelationshipWizardStep1)cntrller.GetFirstStep();
            step.InitialiseStep();
            step.RelatedClassComboBox.SelectedItem = relatedClass;
            //---------------Test Result -----------------------
            string message;
            Assert.IsTrue(step.CanMoveOn(out message), message);

            Assert.IsInstanceOf(typeof (CreateRelationshipWizardStep1), cntrller.GetCurrentStep());
            Assert.IsInstanceOf(typeof (CreateRelationshipWizardStep1), cntrller.GetFirstStep());
            Assert.IsInstanceOf(typeof (CreateRelationshipWizardStep2), cntrller.GetNextStep());
        }
        [Test]
        public void Test_GetNextStep_WhenFirstStep_OwnerClassNullObjectID_ShouldReturnStep2()
        {
            //---------------Set up test pack-------------------
            DMClass relatedClass = TestUtilsDMClass.CreateSavedDMClass();
            DMClass dmClass = TestUtilsDMClass.CreateSavedDMClass(relatedClass.Solution);
            var cntrller = GetController(dmClass);
            //---------------Assert Precondition----------------
            Assert.IsNull(dmClass.ObjectIdentity);
            Assert.IsFalse(dmClass.IsObjectIDValid);
            //---------------Execute Test ----------------------
            CreateRelationshipWizardStep1 step = (CreateRelationshipWizardStep1)cntrller.GetFirstStep();
            step.InitialiseStep();
            step.RelatedClassComboBox.SelectedItem = relatedClass;
            //---------------Test Result -----------------------
            string message;
            Assert.IsTrue(step.CanMoveOn(out message), message);

            Assert.IsInstanceOf(typeof (CreateRelationshipWizardStep1), cntrller.GetCurrentStep());
            Assert.IsInstanceOf(typeof (CreateRelationshipWizardStep1), cntrller.GetFirstStep());
            Assert.IsInstanceOf(typeof (CreateRelationshipWizardStep2), cntrller.GetNextStep());
        }

        [Test]
        public void Test_GetNextStep_WhenFirstStep_RelatedClassNullObjectID_ShouldReturnStep2()
        {
            //---------------Set up test pack-------------------
            DMClass relatedClass = TestUtilsDMClass.CreateSavedDMClass();
            DMClass dmClass = TestUtilsDMClass.CreateSavedDMClass(relatedClass.Solution);
            dmClass.ObjectIdentity = TestUtilsDMObjectIdentity.CreateUnsavedValidDMObjectIdentity_NoDMClass();
            var cntrller = GetController(dmClass);
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClass.ObjectIdentity);
            Assert.IsNull(relatedClass.ObjectIdentity);
            //---------------Execute Test ----------------------
            CreateRelationshipWizardStep1 step = (CreateRelationshipWizardStep1)cntrller.GetFirstStep();
            step.InitialiseStep();
            step.RelatedClassComboBox.SelectedItem = relatedClass;
            //---------------Test Result -----------------------
            string message;
            Assert.IsTrue(step.CanMoveOn(out message), message);

            Assert.IsInstanceOf(typeof (CreateRelationshipWizardStep1), cntrller.GetCurrentStep());
            Assert.IsInstanceOf(typeof (CreateRelationshipWizardStep1), cntrller.GetFirstStep());
            Assert.IsInstanceOf(typeof (CreateRelationshipWizardStep2), cntrller.GetNextStep());
        }

        [Test]
        public void Test_GetNextStep_WhenFirstStep_OwnerClassSelfReferencing_ShouldReturnStep2()
        {
            //---------------Set up test pack-------------------
            //Relate a class to itself- A Part that has a relationship to other Parts
            //Should allow user to map properties as default of PK to PK is meaningless in this situation
            DMObjectIdentity dmObjectIdentity = TestUtilsShared.CreateUnsavedValidObjectIdentityInfo();
            DMClass dmClass = dmObjectIdentity.Class;
            var cntrller = GetController(dmClass);
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClass.ObjectIdentity);
            //---------------Execute Test ----------------------
            CreateRelationshipWizardStep1 step = (CreateRelationshipWizardStep1)cntrller.GetFirstStep();
            step.InitialiseStep();
            step.RelatedClassComboBox.SelectedItem = dmClass;
            //---------------Test Result -----------------------
            string message;
            Assert.IsTrue(step.CanMoveOn(out message), message);

            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep1), cntrller.GetCurrentStep());
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep1), cntrller.GetFirstStep());
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep2), cntrller.GetNextStep());
        }

        #endregion

        #region When Foreign Key Props Found

        //  Look for prop named OwnerClass + ID
        //  if found then
        //   - 1:M cardinality
        //   - RelattionshipName = Pluralise of RelatedClass
        //   - MyRelatedProp = IDProp
        //   - RelatedProp = foundProp
        //  The Remainder are Defaults
        //	== Create Reverse Relationship with names etc.
        //  	-- Show the user the std Relationship screen and allow them to edit anything.
        [Test]
        public void Test_GetNextStep_WhenFirstStep_RelatedClassHasForeignKey_ShouldRetStep3()
        {
            //---------------Set up test pack-------------------
            DMClass ownerClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            TestUtilsShared.CreateSavedObjectIdentityInfo(ownerClass);
            DMClass relatedClass = TestUtilsDMClass.CreateSavedDMClass(ownerClass.Solution);
            TestUtilsShared.CreateSavedObjectIdentityInfo(relatedClass);
            DMProperty property = TestUtilsDMProperty.CreateUnsavedValidDMProperty(relatedClass);
            property.PropertyName = ownerClass.ClassNameBO + "ID";

            var cntrller = GetController(ownerClass);

            CreateRelationshipWizardStep1 step = (CreateRelationshipWizardStep1)cntrller.GetFirstStep();
            step.InitialiseStep();
            step.RelatedClassComboBox.SelectedItem = relatedClass;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(ownerClass.ObjectIdentity);
            Assert.IsNotNull(relatedClass.ObjectIdentity);
            Assert.IsNotNull(relatedClass.GetDMProperty(property.PropertyName)); string message;
            Assert.IsTrue(step.CanMoveOn(out message), message);
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep1), cntrller.GetCurrentStep());
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep1), cntrller.GetFirstStep());
            //---------------Execute Test ----------------------
            IWizardStep actual = cntrller.GetNextStep();
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep3), actual);
        }
        //   - If Related Class has single Prop ID
        //     Look for Prop in OwnerClass = RelatedClass + ID
        //     If found 
        //	- M:1 cardinality
        //   	- RelattionshipName = Singularise of RelatedClass
        //   	- MyRelatedProp = foundProp
        //   	- RelatedProp = RelatedProp.IDProp
        // 	Remainder Default
        [Test]
        public void Test_GetNextStep_WhenFirstStep_OwnerClassHasForeignKey()
        {
            //---------------Set up test pack-------------------
            DMClass ownerClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            TestUtilsShared.CreateSavedObjectIdentityInfo(ownerClass);
            DMClass relatedClass = TestUtilsDMClass.CreateSavedDMClass(ownerClass.Solution);
            TestUtilsShared.CreateSavedObjectIdentityInfo(relatedClass);
            DMProperty property = TestUtilsDMProperty.CreateUnsavedValidDMProperty(ownerClass);
            property.PropertyName = relatedClass.ClassNameBO + "ID";
            var cntrller = GetController(ownerClass);

            CreateRelationshipWizardStep1 step = (CreateRelationshipWizardStep1)cntrller.GetFirstStep();
            step.InitialiseStep();
            step.RelatedClassComboBox.SelectedItem = relatedClass;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(ownerClass.ObjectIdentity);
            Assert.IsNotNull(relatedClass.ObjectIdentity);
            Assert.IsNotNull(ownerClass.GetDMProperty(property.PropertyName)); string message;
            Assert.IsTrue(step.CanMoveOn(out message), message);
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep1), cntrller.GetCurrentStep());
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep1), cntrller.GetFirstStep());
            //---------------Execute Test ----------------------
            IWizardStep actual = cntrller.GetNextStep();
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep3), actual);
        }

        [Test]
        public void Test_GetPreviousStep_WhenStep3_DidNotHaveStep2_ShouldReturnStep1()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateSavedDMClass();
            CreateRelationshipWizardController cntrller = GetControllerWithMocks(dmClass);
            var relationshipCreator = cntrller.RelationshipCreator;
            cntrller.GetFirstStep();
            var step3 = cntrller.GetNextStep();
            //---------------Assert Precondition----------------
            Assert.IsTrue(relationshipCreator.CanFindForeignKey);
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep3), step3);
            //---------------Execute Test ----------------------
            IWizardStep previousStep = cntrller.GetPreviousStep();
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep1), previousStep);
        }

        [Test]
        public void Test_GetNextStep_WhenStep1_PreviouslyReturnedFromStep3_ShouldReturnStep3()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateSavedDMClass();
            CreateRelationshipWizardController cntrller = GetControllerWithMocks(dmClass);
            var relationshipCreator = cntrller.RelationshipCreator;
            cntrller.GetFirstStep();
            var step3 = cntrller.GetNextStep();
            //---------------Intermediate Asserts---------------
            Assert.IsTrue(relationshipCreator.CanFindForeignKey);
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep3), step3);
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep3), cntrller.GetCurrentStep());
            //--------------Move Back---------------------------
            var previousStep = cntrller.GetPreviousStep();
            //---------------Assert Precondition---------------
            Assert.IsTrue(relationshipCreator.CanFindForeignKey);
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep1), previousStep);
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep1), cntrller.GetCurrentStep());
            //---------------Execute Test ----------------------
            var finalStep = cntrller.GetNextStep();
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep3), finalStep);
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep3), cntrller.GetCurrentStep());
        }

        [Test]
        public void Test_GetNextStep_WhenStep1_PreviouslyReturnedFromStep3_WhenChangedChoiceAndNow_ShouldReturnStep2()
        {
            //---------------Set up test pack------------------
            DMClass dmClass = TestUtilsDMClass.CreateSavedDMClass();
            CreateRelationshipWizardController cntrller = GetControllerWithMocks(dmClass);
            var relationshipCreator = cntrller.RelationshipCreator;
            cntrller.GetFirstStep();
            var step3 = cntrller.GetNextStep();
            //---------------Intermediate Asserts---------------
            Assert.IsTrue(relationshipCreator.CanFindForeignKey);
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep3), step3);
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep3), cntrller.GetCurrentStep());
            //--------------Move Back---------------------------
            var previousStep = cntrller.GetPreviousStep();
            //---------------Assert Precondition---------------
            Assert.IsTrue(relationshipCreator.CanFindForeignKey);
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep1), previousStep);
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep1), cntrller.GetCurrentStep());
            //---------------Execute Test ----------------------
            relationshipCreator.ClearStubReturnValues();
            relationshipCreator.Stub(creator => creator.CanFindForeignKey).Return(false);
            Assert.IsFalse(relationshipCreator.CanFindForeignKey);
            IWizardStep nextStep = cntrller.GetNextStep();
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep2), nextStep);
        }

        [Test]
        public void Test_GetNextStep_WhenFirstStep_WhenIsRelationshipSetup_ShouldReturnStep3()
        {
            //---------------Set up test pack-------------------
            DMClass relatedClass = TestUtilsDMClass.CreateSavedDMClass();
            DMClass dmClass = TestUtilsDMClass.CreateSavedDMClass(relatedClass.Solution);
            CreateRelationshipWizardController cntrller = GetControllerWithMocks(dmClass);
            //---------------Assert Precondition----------------
            Assert.IsTrue(cntrller.RelationshipCreator.CanFindForeignKey);

            CreateRelationshipWizardStep1 step = (CreateRelationshipWizardStep1)cntrller.GetFirstStep();
            step.InitialiseStep();
            step.RelatedClassComboBox.SelectedItem = relatedClass;

            string message;
            Assert.IsTrue(step.CanMoveOn(out message), message);

            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep1), cntrller.GetCurrentStep());
            //---------------Execute Test ----------------------
            var nextStep = cntrller.GetNextStep();
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(CreateRelationshipWizardStep3), nextStep);
        }



        private static IUnityContainer CreateContainer()
        {
            var container = new UnityContainer();
            container.RegisterType<DMRelationshipCreator, DMRelationshipCreator>();
            return container;
        }

        private CreateRelationshipWizardController GetController(DMClass dmClass)
        {
            var container = CreateContainer();
            return new CreateRelationshipWizardController(container, dmClass);
        }

        private static CreateRelationshipWizardController GetControllerWithMocks(DMClass dmClass)
        {
            var container = new UnityContainer();
            var mockCreator = MockRepository.GenerateMock<DMRelationshipCreator>(dmClass);
            mockCreator.Stub(creator => creator.CanFindForeignKey).Return(true);
            container.RegisterInstance(mockCreator);
            return new CreateRelationshipWizardController(container, dmClass);
        }

        #endregion
    }

}